home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 151-175 / disk_166 / autograf / readfile.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  7KB  |  290 lines

  1.  
  2. /*
  3.      readfile.c : read the autograf data file into internal storage
  4.  
  5.      By Joel Swank September 3, 1988
  6.      Version 1.0
  7.  
  8.      */
  9.  
  10. #include <exec/types.h>
  11. #include <intuition/intuition.h>
  12. #include <stdio.h>
  13. #include <fcntl.h>
  14. #include <ctype.h> 
  15. #include "fileio.h"
  16.  
  17. #define ARRAYSIZE 10     /* sizes for running averages arrays */
  18. #define ODARRAYSIZE 11
  19.  
  20. /*************
  21.  
  22.  *  Hooks to the rest of the program
  23.  
  24.  *************/
  25.  
  26. extern struct Window    *Wind;
  27. extern struct FileIOSupport *FIOSupp;
  28.  
  29. /* save areas for graphing data */
  30. extern int yrsave[1000];        /* save year of each entry */
  31. extern float costsave[1000];    /* save averaged cost data of each entry */
  32. extern float milesave[1000];    /* save averaged mile data of each entry */
  33. extern float pricesave[1000];    /* save price data of each entry */
  34. extern float odsave[1000];        /* save odometer data of each entry */
  35. extern float rawcost[1000];        /* save cost data of each entry */
  36.  
  37.  
  38. extern struct IntuiText oktxt;
  39. extern struct IntuiText FioText1;
  40. extern struct RastPort *rp;
  41. extern int numrecs;
  42.  
  43. /* Local Data */
  44.  
  45. char linebuf[200];
  46. char linenum[40];
  47.  
  48. struct IntuiText ffmtmsg3 = {
  49.     0,1,JAM2,    /* front and back text pens, drawmode and fill byte */
  50.     14,30,    /* XY origin relative to container TopLeft */
  51.     NULL,    /* font pointer or NULL for default */
  52.     (UBYTE *) linebuf,    /* pointer to text */
  53.     NULL    /* next IntuiText structure */
  54. };
  55.  
  56. struct IntuiText ffmtmsg2 = {
  57.     0,1,JAM2,    /* front and back text pens, drawmode and fill byte */
  58.     14,20,    /* XY origin relative to container TopLeft */
  59.     NULL,    /* font pointer or NULL for default */
  60.     (UBYTE *) linenum,    /* pointer to text */
  61.     &ffmtmsg3    /* next IntuiText structure */
  62. };
  63.  
  64. struct IntuiText ffmtmsg = {
  65.     0,1,JAM2,    /* front and back text pens, drawmode and fill byte */
  66.     14,10,    /* XY origin relative to container TopLeft */
  67.     NULL,    /* font pointer or NULL for default */
  68.     (UBYTE *)"File has format error",    /* pointer to text */
  69.     &ffmtmsg2    /* next IntuiText structure */
  70. };
  71.  
  72. char filename[80];
  73.  
  74. struct IntuiText openfimsg2 = {
  75.     0,1,JAM2,    /* front and back text pens, drawmode and fill byte */
  76.     14,26,    /* XY origin relative to container TopLeft */
  77.     NULL,    /* font pointer or NULL for default */
  78.     (UBYTE *) filename,
  79.     NULL    /* next IntuiText structure */
  80. };
  81.  
  82. struct IntuiText openfimsg = {
  83.     0,1,JAM2,    /* front and back text pens, drawmode and fill byte */
  84.     14,16,    /* XY origin relative to container TopLeft */
  85.     NULL,    /* font pointer or NULL for default */
  86.     (UBYTE *)"Can't open file:",    /* pointer to text */
  87.     &openfimsg2    /* next IntuiText structure */
  88. };
  89.  
  90. struct IntuiText cantxt = {
  91.     2,1,JAM2,    /* front and back text pens, drawmode and fill byte */
  92.     5,3,    /* XY origin relative to container TopLeft */
  93.     NULL,    /* font pointer or NULL for default */
  94.     (UBYTE *)"CANCEL",    /* pointer to text */
  95.     NULL    /* next IntuiText structure */
  96. };
  97.  
  98. struct IntuiText retrytxt = {
  99.     2,1,JAM2,    /* front and back text pens, drawmode and fill byte */
  100.     5,3,    /* XY origin relative to container TopLeft */
  101.     NULL,    /* font pointer or NULL for default */
  102.     (UBYTE *)"RETRY",    /* pointer to text */
  103.     NULL    /* next IntuiText structure */
  104. };
  105.  
  106. /* arrays to save data for running averages */
  107. float tengal[ARRAYSIZE];
  108. float tenod[ODARRAYSIZE];
  109. float tencost[ARRAYSIZE];
  110. int odi, gali, costi;    /* indices for above */
  111.  
  112. int fileflag;
  113. int fileread = FALSE;
  114. int count, reccnt ;
  115. int year;
  116. char reqtitle[] = "Select an Input Data File";
  117. FILE *fopen(), *fp;
  118. float firstod = 0, miles, od;
  119. float lastod, atof();
  120. float gallons, cost, price;
  121.  
  122. /*
  123.  * read_file : Read one file into memory
  124.  */
  125.  
  126. read_file()
  127. {
  128.    char *pp, *tp;
  129.    int i, elapsemiles, stopi;
  130.    float temp ;
  131.    float previous = 0;
  132.    int previousyr = 0;
  133.  
  134.    SetWindowTitles(Wind,(UBYTE *) "AutoGraf File Selection", -1L);
  135.    clr_grf();
  136.    PrintIText(rp,&FioText1,0L,0L);    /* Print fileio text */
  137.    FIOSupp->ReqTitle = (UBYTE *) reqtitle;
  138.  
  139.    Retry:
  140.    if (GetFileIOName(FIOSupp,Wind))
  141.         BuildFileIOPathname(FIOSupp,filename);
  142.    else return;
  143.  
  144.    while ((fp = fopen(filename,"r")) == NULL)
  145.         {
  146.         if (AutoRequest(Wind,&openfimsg,&retrytxt,&cantxt,0L,0L,300L,75L))
  147.             continue;
  148.         goto Retry;
  149.         }
  150.  
  151.  
  152.    odi = 0;
  153.    gali = 0;
  154.    costi = 0;
  155.  
  156. /*************************************************
  157.      Read data and save in arrays;
  158. *************************************************/
  159.  
  160.    clr_grf();
  161.    SetWindowTitles(Wind,(UBYTE *) "AutoGraf Reading Data file", -1L);
  162.    SetWaitPointer(Wind);
  163.    Move(rp,150L,100L);
  164.    Text(rp,"READING FILE ",13L);
  165.    Text(rp,filename,strlen(filename));
  166.  
  167.    count = 0;
  168.    reccnt = 0;
  169.    while (0 != fgets(linebuf,100,fp))
  170.       {
  171.       if (count >= 1000) break;    /* the limit - needs error info */
  172.  
  173.       reccnt++;
  174.       pp = linebuf;
  175.       while (*(++pp) != ',') if (badchk(*pp)) return ;    /* to end of date */
  176.  
  177.       pp -= 4;
  178.       year = atoi(pp);    /* get year */
  179.       if (previousyr)
  180.           {
  181.           if (year != previousyr && year != previousyr+1 )
  182.               {
  183.             badchk('\0');    /* force format error */
  184.             return;
  185.             } else previousyr = year;
  186.         } else previousyr = year;
  187.  
  188.       pp += 5;            /* past comma */
  189.  
  190.       od = atof(pp);    /* get odometer reading */
  191.       if (od < previous)
  192.           {
  193.         badchk('\0');    /* force format error */
  194.         return;
  195.         } else previous = od;
  196.  
  197.       if (!firstod) firstod = od;
  198.       lastod = od;
  199.       tenod[odi++] = od;
  200.       if (odi == ODARRAYSIZE) odi=0;
  201.       pp++;
  202.  
  203.       while (*(++pp) != ',') if (badchk(*pp)) return ;
  204.       while (*(++pp) != '$') if (badchk(*pp)) return ;
  205.       pp++;
  206.       price = atof(pp);        /* get price */
  207.  
  208.       while (*(++pp) != ',') if (badchk(*pp)) return ;
  209.       while (*(++pp) != '$') if (badchk(*pp)) return ;
  210.       pp++;
  211.       cost = atof(pp);        /* get cost */
  212.       if (reccnt == 1) cost = 0;    /* throw out first one */
  213.       tencost[costi++] = cost;
  214.       if (costi == ARRAYSIZE) costi=0;
  215.  
  216.       gallons = cost/price;    /* get gallons  */
  217.       if (reccnt == 1) gallons = 0;    /* throw out first one */
  218.       tengal[gali++] = gallons;
  219.       if (gali == ARRAYSIZE) gali=0;
  220.  
  221.  
  222.  
  223.       if (reccnt > 10) 
  224.           elapsemiles =(tenod[(odi-1 > 0) ? odi-1 : 10]-tenod[odi]);
  225.       else
  226.           elapsemiles = tenod[odi-1] - tenod[0];
  227.  
  228.       if (reccnt > 10) stopi = 10;
  229.       else stopi = reccnt;
  230.       /* calc cost/mile for last 10 entrys */
  231.       temp = 0;
  232.       if (elapsemiles != 0)
  233.           {
  234.         for (i=0; i<stopi; i++) temp += tencost[i];
  235.         temp = temp/elapsemiles;
  236.         temp *= 100; /* make it cents per mile */
  237.         }
  238.       costsave[count] = temp;
  239.  
  240.         /* calc miles/gallon for last 10 entrys */
  241.       temp = 0;
  242.       if (elapsemiles != 0)
  243.           {
  244.         for (i=0; i<stopi; i++) temp += tengal[i];
  245.         if (temp == 0)
  246.             {
  247.             badchk('\0');    /* force file format error */
  248.             return;
  249.             }
  250.         temp = elapsemiles/temp;
  251.         }
  252.       milesave[count] = temp;
  253.  
  254.       yrsave[count] = year;
  255.       odsave[count] = od;
  256.       rawcost[count] = cost;
  257.       pricesave[count] = price;
  258.  
  259.       count++;
  260.       }    /* end of read a line loop */
  261.  
  262.       fclose(fp);
  263.  
  264.       numrecs = count;
  265.       ClearPointer(Wind);
  266.  
  267.       /* enable other two menu items */
  268.       OnMenu(Wind, (USHORT) SHIFTITEM(1));
  269.       OnMenu(Wind, (USHORT) SHIFTITEM(2));
  270.       fileread = TRUE;
  271. }
  272.  
  273. /*
  274.  * badchk : check for invalid character - cleanup and return
  275.  *          TRUE if found, else return FALSE
  276.  */
  277.  
  278. badchk(ch)
  279. char ch;
  280. {
  281.     if ( isprint(ch) ) return(FALSE);
  282.     sprintf(linenum,"Line number %d:",reccnt);
  283.     linebuf[32] = '\0'; /* Limit to requester width */
  284.     AutoRequest(Wind,&ffmtmsg,0L,&oktxt,0L,0L,300L,75L);
  285.     ClearPointer(Wind);
  286.     fclose(fp);
  287.     return(TRUE);
  288. }
  289.  
  290.